<template>
  <div class="app-container">
    <div class="ces-main">
      <div class="main" style="width: 48vw;margin-right: 4px;">
        <Search-Form ref="form" size="mini" labelWidth="50px" :searchData="SearchWO" :tableCols="tableCols"
          :searchHandle="searchHandle" @handleEnter="refresh"></Search-Form>
        <v-GetToolBar class="tool" align="right" :MenuCode="MenuCode" :IsfunEdit="IsfunEdit" :Isfunfail="Isfunfail"
          @handeAdd="handeAdd" @handleEdit="handleEdit" @OpenFindForm="OpenFindForm" @handlexportData="handlexportData"
          @IsfunEdit="handleIsfunEdit" @IsfunDelete="handleIsfunfail" @handleclose="handleclose"></v-GetToolBar>

        <ComVxeTable class="vxe" size="small" :isSelection="false" :isIndex="true" :isSearch="true" :isHandle="false"
          :ToolBarheight="Tool1" :IsfunEdit="false" :Isfunfail="false" :tableData="tableData"
          :tableCols="tableCols" :Search="SearchWO" @refresh="refresh" @handleEdit="handleEdit" @select="handselect"
          @selectAll="handselectAll" @handleDelete="delData" @handrowclick="handrowclick">
        </ComVxeTable>
      </div>

      <div class="main more" style="width: 38vw;">
        <Search-Form class="searAbs" ref="form" size="mini" labelWidth="50px" :searchData="SearchWO" :tableCols="tableCols"
          :searchHandle="searchHandle" @handleEnter="refresh"></Search-Form>
        <v-GetToolBar class="tool" align="right" :MenuCode="MenuCode" :IsfunEdit="IsfunEdit" :Isfunfail="Isfunfail"
          @handeAdd="handeAdd" @handleEdit="handleEdit" @OpenFindForm="OpenFindForm" @handlexportData="handlexportData"
          @IsfunEdit="handleIsfunEdit" @IsfunDelete="handleIsfunfail" @handleclose="handleclose"></v-GetToolBar>
        <ComVxeTable class="vxe" size="mini" :isSelection="false" :isIndex="true" :isSearch="true" :isHandle="false"
          :ToolBarheight="ToolBarheight * 2.2" :IsfunEdit="true" :Isfunfail="true" :tableData="tableDataCode"
          :tableCols="tableColsCode" :Search="SearchSSN" @refresh="refresh" @handleEdit="handleEdit"
          @select="handselectCode" @selectAll="handselectAllCode" @handleDelete="delData"
          @handrowclick="handrowclickCode">
        </ComVxeTable>
        <div style="height: 30px;"></div>
        <i class="el-icon-arrow-up black" @click="toTop"></i>
        <ComVxeTable class="vxe" :widthBton="70" size="mini" :isSelection="true" :isIndex="true" :isSearch="true"
          :isHandle="false" :ToolBarheight="ToolBarheight * 2.2" :IsfunEdit="false" :Isfunfail="false"
          :tableData="tableDataThree" :tableCols="tableColsThree" :Search="SearchThree" @refresh="refresh"
          @handleEdit="handleEdit" @select="handselectThree" @selectAll="handselectThree" @handleDelete="delData"
          @handrowclick="handrowclickCode">
        </ComVxeTable>
      </div>
    </div>
    <el-dialog v-if="dialogVisible" :visible.sync="dialogVisible" width="800px" top="20px">
      <el-form :model="name" label-width="80px" label-position="right">
        <el-tabs type="border-card">
          <el-tab-pane label="">
            <el-form :model="FormData" label-width="90px" ref="MianForm">
              <Mian-Form ref="form" labelWidth="100px" :FormData="FormData" :tableCols="tableColsCode" :StepSN="0"
                :searchHandle="searchHandle"></Mian-Form>
            </el-form>
          </el-tab-pane>
          <div style="float:right ">
            <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
            <el-button type="primary" @click="handleSave">保存</el-button>
          </div>
        </el-tabs>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
//引入组件

import ComVxeTable from "../Common//ComVxeTable.vue";
import SearchForm from "../Common//Search.vue";
import MianForm from "../Common//form.vue";
//, getDataList,getTableCols  #78fff7
import { funDownload, GetTableColsData } from "@/api/Common";

import { getLodop } from "@/api/LodopFuncs";

import {
  getDataList,
  DeleteForm,
  UpdateForm,
} from "@/api/CommonPage/CommonPage"; //更改
import { MESSSN, MESSSNUpdate } from "@/api/Collection/BarcodePrinting"; //更改

import { CloseForm } from "@/api/System";
import vGetToolBar from "../Common/GetToolBar";

export default {
  //Collection/BarcodePrinting
  name: "Prior",
  data() {
    //表单查询一般分为input，下拉框，和日期，下面是下拉框时的设置下拉信息的情况，我的项目没有用到，但是留着方便看

    return {
      FormData: {
        IsMain: "",
      },
      tableColsFrom: [],
      WO: "",
      PrdName: "",
      radio: "3",
      row: [],
      createQty: null,
      count: null,
      selectCode: [],
      SelectData: {},
      reCode: {},
      tableDataCode: [],
      tableColsCode: [],
      tableDataThree: [],
      tableColsThree: [],
      formInline: {
        user: "",
        region: "",
      },
      MenuCode: "A051114",
      tableData: [], //表格数据
      tableCols: [], //表格列数据
      searchHandle: [],
      billID: "",
      IsfunEdit: false,
      Isfunfail: true,
      SearchWO: {
        // 查询条件
        total: 0, // 总记录数
        pageSize: 20, // 一页显示的记录数
        page: 1, // /当前页码
        RNO: "1633", // 表编号  根据Controller中对应的表编号
        InterID: "1633",
        jsWhere: "", // 自定义条件
        IsFieldText: "0",
        Download: "",
        // Status: 0,
        //   BillState: "BillStateALL",
      },
      SearchSSN: {
        // 查询条件
        total: 0, // 总记录数
        pageSize: 20, // 一页显示的记录数
        page: 1, // /当前页码
        RNO: "1635", // 表编号  根据Controller中对应的表编号
        InterID: "1635",
        jsWhere: "", // 自定义条件
        IsFieldText: "0",
        Download: "",
        // Status: 0,
        //   BillState: "BillStateALL",
      },
      SearchThree: {
        // 查询条件
        total: 0, // 总记录数
        pageSize: 20, // 一页显示的记录数
        page: 1, // /当前页码
        RNO: "1634", // 表编号  根据Controller中对应的表编号
        InterID: "1634",
        jsWhere: "", // 自定义条件
        IsFieldText: "0",
        Download: "",
        // Status: 0,
        //   BillState: "BillStateALL",
      },
      SearchFour: {
        // 查询条件
        total: 0, // 总记录数
        pageSize: 20, // 一页显示的记录数
        page: 1, // /当前页码
        RNO: "1636", // 表编号  根据Controller中对应的表编号
        InterID: "1636",
        jsWhere: "", // 自定义条件
        IsFieldText: "0",
        Download: "",
        // Status: 0,
        //   BillState: "BillStateALL",
      },

      SearchDialog: false,
      DialogBtnMargin: 0,
      dialogVisible: false,
      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      ToolBarheight: "",
      saveProcessID: "",
      selectThree: [],
      rowSave: {},
      saveRowClick: {},
      Tool1:'',
    };
  },

  components: {
    //引入组件后注册组件
    vGetToolBar,
    SearchForm,
    MianForm,
    ComVxeTable,
  },
  mounted() {
    this.ToolBarheight = parseInt(document.documentElement.clientHeight * 0.295);
    this.Tool1 = parseInt(document.documentElement.clientHeight * 0.23);
    this.getDataList();
  },
  methods: {
    handselectThree(data) {
      this.selectThree = data;
    },
    async toTop() {
      let flag = false
      if (this.tableDataCode.length == 0) flag = true
      this.selectThree.forEach(async (obj, index) => {
        // this.SearchSSN.MaterialID = this.saveRowClick.MaterialID;
        this.SearchSSN.ProcessID = this.saveProcessID;
        this.SearchSSN.OrganizeID = obj.organizeid;
        const result = await getDataList({
          data: JSON.stringify(this.SearchSSN),
        });

        // this.SearchSSN.MaterialID = "";
        this.SearchSSN.ProcessID = "";
        this.SearchSSN.OrganizeID = "";

        let data = result.data.Table[0] || { ProcessMaterial: "" };

        data.ProcessID = this.saveProcessID;
        data.OrganizeID = obj.organizeid;
        // data.OrganizeID = obj.organizeid;
        data.Priority = "1";
        data.StandardPeoples = "1";
        data.Points = "0.00";
        data.IsMain =  false;
        data.MaterialID = this.saveRowClick.MaterialID;

        // if (!this.tableDataCode.some((v) => v.OrganizeID === data.OrganizeID)) {
        await UpdateForm({
          IsPostBack: true,
          InterID: this.SearchSSN.InterID,
          SavaDate: JSON.stringify(data),
        });

        // }

        if (index == this.selectThree.length - 1) {
          if (flag) {
            // debugger //删除没删关联表
            this.SearchFour.OrganizeID = obj.organizeid;
            this.SearchFour.MaterialID = this.saveRowClick.MaterialID;
            this.SearchFour.ProcessID = this.saveProcessID;
            const res1 = await getDataList({
              data: JSON.stringify(this.SearchFour),
            });

            // this.SearchFour.MaterialID = "";
            // this.SearchFour.ProcessID = "";
            // this.SearchFour.OrganizeID = "";

            let Data = res1.data.Table[0] || { SchedulingSpecialID: "" };

            // Data.SchedulingSpecialID = "SC20240413001";
            Data.OrganizeID = obj.organizeid;
            Data.MaterialID = this.saveRowClick.MaterialID;
            Data.IsMain =  true;
            Data.ProcessID = this.saveProcessID;
            const result = await UpdateForm({
              IsPostBack: true,
              InterID: this.SearchFour.InterID,
              SavaDate: JSON.stringify(Data),
            });
            // debugger
            // this.FormData = result.data1.Table[0];
            if (result.code === 20000) {
              this.$notify({
                title: result.msg,
                dangerouslyUseHTMLString: true,
                message: "",
                type: "success",
              });
              // const reCode = await getDataList({
              //   data: JSON.stringify(this.SearchSSN),
              // });
              // this.tableDataCode = reCode.data.Table;
            } else {
              this.$notify({
                title: result.msg,
                dangerouslyUseHTMLString: true,
                message: "",
                type: "error",
              });
            }
            //   this.getDataList();
            this.dialogVisible = false;
          }
          const res = await getDataList({
            data: JSON.stringify(this.SearchSSN),
          });
          this.tableDataCode = res.data.Table;
          this.SearchSSN.total = res.data.Table1[0].RecordCount;
        }
      });
    },
    async searchWo() {
      const res = await getDataList({ data: JSON.stringify(this.SearchWO) });
      this.tableData = res.data.Table;
      this.Search.total = res.data.Table1[0].RecordCount;
    },
    handleInput(value) {
      if (value == 1) {
        this.tableDataCode = this.tableDataCodeSave.filter(
          (v) => v.PRINTEDTIMES == 1
        );
      } else {
        this.tableDataCode = this.tableDataCodeSave.filter(
          (v) => v.PRINTEDTIMES != 1
        );
      }
    },
    createBarcode() {
      if (Object.keys(this.SelectData).length === 0) {
        this.$message.warning("请先选择数据");
        return;
      }
      this.dialogVisible = true;
    },

    handselectCode(row) {
      this.selectCode = row;
    },
    onSubmit() { },
    //搜索重置
    refresh() {
      this.getDataListCe();
    },
    reform() {
      this.Search = {
        // 查询条件
        // total: 0, // 总记录数
        pageSize: 14, // 一页显示的记录数
        page: 1, // /当前页码
        RNO: "4", // 表编号  根据Controller中对应的表编号
        jsWhere: "", // 自定义条件
      };
      this.getDataListCe();
    },
    async getDataList() {
      const res = await getDataList({ data: JSON.stringify(this.SearchWO) });
      this.SearchWO.IsFieldText = "1"; //已加载数据，不再加载
      // IsFieldText: "0",
      this.tableData = res.data.Table;
      this.tableCols = res.data1;
      this.SearchWO.total = res.data.Table1[0].RecordCount;

      this.SearchSSN.jsWhere = `and  1 = 2`;
      const resCols = await getDataList({
        data: JSON.stringify(this.SearchSSN),
      });
      this.SearchSSN.IsFieldText = "1"; //已加载数据，不再加载
      this.tableColsCode = resCols.data1;
      this.SearchSSN.total = resCols.data.Table1[0].RecordCount;

      this.SearchThree.jsWhere = `and  1 = 2`;
      const resC = await getDataList({
        data: JSON.stringify(this.SearchThree),
      });
      this.SearchThree.IsFieldText = "1"; //已加载数据，不再加载
      this.tableColsThree = resC.data1;
      this.SearchThree.total = resC.data.Table1[0].RecordCount;
    },
    async OpenFindForm() {
      //打开搜索框
      // this.SearchDialog = true;
      //计算外边距
      // console.log(this.$refs.searchDialog.offsetHeight);
      // this.DialogBtnMargin = this.$refs.searchDialog.offsetHeight - 40;

      const res = await getDataList({ data: JSON.stringify(this.SearchWO) });
      this.SearchWO.total = res.data.Table1[0].RecordCount;
      this.tableData = res.data.Table;
    },
    handlexportData() {
      funDownload("334", JSON.stringify(this.Search));
    },
    handleIsfunEdit() {
      this.IsfunEdit = true;
    },
    handleIsfunfail() {
      this.Isfunfail = true;
    },
    async handeAdd() {
      // 新建

      let routeUrl = this.$router.push({
        path: "/System/PageAutoForm",
        query: { billID: "", IsPostBack: false },
      });
      window.open(routeUrl.path, "_top");
    },
    async handleEdit({ row }) {
      // 修改
      //   this.tableColsFrom = this.tableColsCode;
      this.dialogVisible = true;
      const result = await UpdateForm({
        IsPostBack: false,
        InterID: this.SearchSSN.InterID,
        SavaDate: JSON.stringify(row),
      });
      this.FormData = result.data1.Table[0];

      this.rowSave = row;
    },
    delData({ $index, row }) {
      this.$confirm("删除操作, 是否继续?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
      })
        .then(async () => {
          // debugger
          // const result = await DeleteForm({
          //     billID: row.SSN,
          //     types: 1,
          //     IsPostBack: false,
          // });

          const result = await DeleteForm({
            InterID: this.SearchSSN.InterID,
            SavaDate: JSON.stringify(row),
            IsPostBack: false,
          });

          // this.getDataListCe();
          this.$message({
            type: "success",
            message: result.msg,
          });
          const res = await getDataList({
            data: JSON.stringify(this.SearchSSN),
          });
          this.tableDataCode = res.data.Table;
          this.SearchSSN.total = res.data.Table1[0].RecordCount;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    async handrowclick({ row }) {
      if (row) {
        this.SearchSSN.MaterialID = row.MaterialID;
      }
      this.SearchSSN.jsWhere = "";
      //上
      const reCode = await getDataList({
        data: JSON.stringify(this.SearchSSN),
      });

      this.tableDataCode = reCode.data.Table;
      this.SearchSSN.total = reCode.data.Table1[0].RecordCount;

      //下
      this.SearchThree.ProcessID = row.ProcessID;
      this.SearchThree.jsWhere = "";
      const reC = await getDataList({
        data: JSON.stringify(this.SearchThree),
      });

      this.tableDataThree = reC.data.Table;
      this.SearchThree.total = reC.data.Table1[0].RecordCount;

      this.saveProcessID = row.ProcessID;
      this.saveRowClick = row;
    },
    // handrowclickCode(row) {
    //     this.CodeOne = row.row
    //     // alert(this.CodeOne.SSN)
    // },
    async handleSave() {
      // 修改
      this.SearchFour.OrganizeID = this.rowSave.OrganizeID
      this.SearchFour.MaterialID = this.rowSave.MaterialID;
      this.SearchFour.ProcessID = this.rowSave.ProcessID;
      const res1 = await getDataList({
        data: JSON.stringify(this.SearchFour),
      });

      // this.SearchFour.MaterialID = "";
      // this.SearchFour.ProcessID = "";
      // this.SearchFour.OrganizeID = "";

      let Data = res1.data.Table[0] || { SchedulingSpecialID: "" };

      // Data.SchedulingSpecialID = "SC20240413001";
      Data.OrganizeID = this.rowSave.OrganizeID; //没 新有
      Data.MaterialID = this.rowSave.MaterialID;
      Data.IsMain = this.FormData.IsMain;
      Data.ProcessID = this.rowSave.ProcessID;
      const result = await UpdateForm({
        IsPostBack: true,
        InterID: this.SearchFour.InterID,
        SavaDate: JSON.stringify(Data),
      });
      // debugger
      // this.FormData = result.data1.Table[0];
      if (result.code === 20000) {
        this.$notify({
          title: result.msg,
          dangerouslyUseHTMLString: true,
          message: "",
          type: "success",
        });
        // const reCode = await getDataList({
        //   data: JSON.stringify(this.SearchSSN),
        // });
        // this.tableDataCode = reCode.data.Table;
      } else {
        this.$notify({
          title: result.msg,
          dangerouslyUseHTMLString: true,
          message: "",
          type: "error",
        });
      }
      //   this.getDataList();
      this.dialogVisible = false;
    },
    handleclose() {
      if (this.billID != "") {
        this.$confirm("关单操作, 是否继续? 订单号：" + this.billID, "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
        })
          .then(async () => {
            const result = await CloseForm({
              billID: this.billID,
              types: 1,
              IsPostBack: false,
            });
            this.getDataListCe();
            this.$message({
              type: "success",
              message: result.msg,
            });
          })
          .catch((err) => {
            console.error(err);
          });
      } else {
        this.$notify({
          title: "关单操作，请选择数据" + this.billID,
          dangerouslyUseHTMLString: true,
          message: "",
          type: "error",
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.ces-main {
  display: flex;
  flex-wrap: nowrap;
  height: 100%;
  .more{
    /deep/ .more{
      transform: translate(-50px, 2.3vh) !important;
    }
  }

  // width: 98vw;
  .botn {
    margin: 2px 0 2px 2px;
  }

  .main {
    flex: 1;
    background-color: #fff;
    border-radius: 4px;
    padding: 4px;
    margin-bottom: 4px;
    height: 100%;
    position: relative;
    .searAbs{
      // position: absolute;
    }

    .black {
      background-color: #000;
      border-radius: 50%;
      color: white;
      position: absolute;
      left: 50%;
      transform: translateY(-18px);
    }

    .tool {
      position: absolute;
      right: 2px;
      top: 12px;
    }

    .inp {
      float: right;
      // display: inline-block;
      width: 100px;
      margin-left: 2px;
      transform: translateY(2px);
    }

    /deep/ .el-radio__label {
      font-size: 12px;
    }

    .check {
      margin-left: 12px;
      transform: translateY(2px);
      margin-right: 0;
      padding-top: 8px;
      width: 66px;
    }

    /deep/ .vxe .el-input__inner {
      height: 22px;
    }

    .float {
      float: right;
    }
  }
}

.dialog {
  /deep/ .el-dialog {
    margin-top: 50vh !important;
    transform: translateY(-50%);
  }

  table {
    width: 100%;
    border-collapse: collapse;

    /* 合并相邻的边框 */
    td {
      border: 1px solid #000;
      padding: 15px;
      /* 添加内边距 */
      text-align: left;
      /* 文字左对齐 */
    }

    tr td:first-child {
      width: 30%;
    }
  }
}

.app-container {
  background-color: rgb(255, 255, 255);
  // padding-bottom: 100px;

  .EditObjs-table {
    margin-top: 30px;
  }

  .permission-tree {
    margin-bottom: 5px;
  }
}

.searchDialog ::v-deep .el-dialog__header {
  background: #409eff;
}

.searchDialog ::v-deep .el-dialog__title {
  color: #fff;
}

.searchDialog ::v-deep .el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}
</style>
